
import { Player } from "../player";

// src/actions/action-manager.ts  
export interface Action {  
  name: string;  
  duration: number;  
  startTime: number;  
  isActive: boolean;  
  update(player: Player, deltaTime: number): void;  
  onStart(player: Player): void;  
  onEnd(player: Player): void;  
}  
  
export class ActionManager {  
  private currentAction: Action | null = null;  
  private actions = new Map<string, Action>();  
    
  registerAction(action: Action) {  
    this.actions.set(action.name, action);  
  }  
    
  startAction(actionName: string, player: Player) {  
    if (this.currentAction) {  
      this.currentAction.onEnd(player);  
    }  
      
    const action = this.actions.get(actionName);  
    if (action) {  
      this.currentAction = action;  
      action.startTime = player.engine.time_;  
      action.isActive = true;  
      action.onStart(player);  
    }  
  }  
    
  update(player: Player) {  
    if (this.currentAction && this.currentAction.isActive) {  
      const elapsed = player.engine.time_ - this.currentAction.startTime;  
        
      if (elapsed >= this.currentAction.duration) {  
        this.currentAction.onEnd(player);  
        this.currentAction = null;  
      } else {  
        this.currentAction.update(player, elapsed);  
      }  
    }  
  }  
    
  getCurrentAction(): string | null {  
    if(!this.currentAction){
        return null;
    }
    return this.currentAction.name || null;  
  }  
}